package id.xsis.project.blog.dao;

import id.xsis.project.blog.entity.Artikel;
import id.xsis.project.blog.entity.User;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("databaseDao")
@Transactional
public class DatabaseDaoImp implements DatabaseDao {

	@Autowired
	private SessionFactory sessionFactory;

	// IMPLEMENTATION FOR ENTITY BLOG
	@SuppressWarnings("unchecked")
	@Override
	public List<Artikel> listArtikel() {
		return sessionFactory.getCurrentSession().createQuery("FROM Artikel")
				.list();
	}

	@Override
	public Artikel findArtikelById(Integer artikel_id) {
		return (Artikel) sessionFactory.getCurrentSession().get(Artikel.class,
				artikel_id);
	}

	@Override
	public Integer findArtikelIdByArtikel(String artikel) {
		StringBuilder query = new StringBuilder();
		query.append("SELECT a.artikel_id FROM Artikel a ");
		query.append("WHERE a.judul = :artikel");
		return (Integer) sessionFactory.getCurrentSession()
				.createQuery(query.toString()).setParameter("artikel", artikel)
				.uniqueResult();
	}

	@Override
	@Transactional(readOnly = false)
	public void saveArtikel(Artikel artikel) {
		sessionFactory.getCurrentSession().saveOrUpdate(artikel);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteArtikel(Artikel artikel) {
		sessionFactory.getCurrentSession().delete(artikel);
	}

	// IMPLEMENTATION FOR ENTITY USER

	@SuppressWarnings("unchecked")
	@Override
	public List<User> listUser() {
		return sessionFactory.getCurrentSession().createQuery("FROM User")
				.list();
	}

	@Override
	public User findIdUserById(Integer user_id) {
		return (User) sessionFactory.getCurrentSession().get(User.class,
				user_id);
	}

	@Override
	public Integer findIdUserByUsername(String user) {

		StringBuilder query = new StringBuilder();
		query.append("SELECT u.user_id FROM User u ");
		query.append("WHERE u.username = :user");
		return (Integer) sessionFactory.getCurrentSession()
				.createQuery(query.toString()).setParameter("user", user)
				.uniqueResult();

	}

	@Override
	public void saveUser(User user) {
		sessionFactory.getCurrentSession().saveOrUpdate(user);

	}

	@Override
	public void deleteUser(User user) {
		sessionFactory.getCurrentSession().delete(user);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Artikel> listAll() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery("FROM Artikel")
				.list();
	}

	// IMPLEMENTATION FOR ENTITY USER ROLES
	
	/*@SuppressWarnings("unchecked")
	@Override
	public List<UserRoles> listUserRole() {
		List<UserRoles> ur = null;
		try {
			ur = sessionFactory.getCurrentSession().createQuery("from UserRoles").list();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		return ur;
	}

	@Override
	public void saveUserRole(UserRoles userRoles) {
		sessionFactory.getCurrentSession().saveOrUpdate(userRoles);
		
	}

	@Override
	public void deleteUserRole(UserRoles userRoles) {
		sessionFactory.getCurrentSession().delete(userRoles);
		
	}
*/
}
